// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#include "coin2/exchange/base/order/unknown_id_manager.h"

namespace coin2::exchange::base::order {

UnknownIdManagerGuard UnknownIdManager::GetGuard() { return UnknownIdManagerGuard(*this); }

bool UnknownIdManager::IsNeedLog(const std::string& symbol, const std::string& id) {
  // logged before
  if (unknown_logged_ids_.count(id) != 0) {
    return false;
  }

  // first meet
  unknown_logged_ids_.emplace(id);
  if (known_symbols_.count(symbol) == 0) {
    // first query, ignore
    return false;
  }

  return true;
}

bool UnknownIdManagerGuard::IsNeedLog(const std::string& symbol, const std::string& id) {
  known_symbols_tmp_.emplace(symbol);

  return manager_.IsNeedLog(symbol, id);
}

UnknownIdManagerGuard::~UnknownIdManagerGuard() {
  for (const auto& symbol : known_symbols_tmp_) {
    manager_.known_symbols_.emplace(symbol);
  }
}

UnknownIdManagerGuard::UnknownIdManagerGuard(UnknownIdManager& manager) : manager_(manager) {}

}  // namespace coin2::exchange::base::order
